import {Component, Input, OnInit} from "@angular/core";
import {PmMessage, PmMessageDeal} from "@shared/models/PmMessage";
import {FileInfo} from "@shared/models/FileInfo";
import {TestData} from "@shared/utils/TestData";
import {BBYUtils} from "@shared/utils/BBYUtils";
import {NzModalRef} from "ng-zorro-antd/modal/modal-ref";
import {PermissionDetail} from "@shared/models/Permission";
import {UserGroup} from "@shared/models/UserInfo";
import {AuthService} from "@core/service/AuthService";
import {NzModalService} from "ng-zorro-antd/modal";
import {AddUserToGroup} from "@/app/pages/user/pm-req/add-user-to-group";
import {PmMessageService} from "@core/service/PmMessageService";
import {NzMessageService} from "ng-zorro-antd/message";
import {DiskService} from "@core/service/DiskService";
@Component({
  selector: 'bby-pm-req-deal',
  styles: [
    `
      [nz-button] {
        margin-right: 8px;
        margin-top: 12px;
      }
    `
  ],
  templateUrl:'./pm-message-deal.dialog.html',
})
export class PmMessageDealDialog implements OnInit {
  @Input()
  pmReqItem: PmMessage | undefined

  @Input()
  dialogObj: { dialog: NzModalRef | undefined } | undefined

  deal = new PmMessageDeal()
  listLoading = true;
  fileList: FileInfo[] = []
  isAnn = true;
  groupList: UserGroup[] = []
  submitLoading=false;

  constructor(
    private authService: AuthService,
    private diskService: DiskService,
    protected dialogService: NzModalService,
    protected message: NzMessageService,
    public pmService:PmMessageService
  ) {
  }

  ngOnInit(): void {
    if (!this.pmReqItem?.fileIds) {
      return;
    }
    if (this.pmReqItem.from) {
      this.isAnn = false
    }

    let userInfo = this.authService.getUserInfo();
    if (userInfo && userInfo.userGroups) {
      this.groupList = Array.from(userInfo.userGroups.values())
    }
    if (this.pmReqItem.deal?.agree){
      this.deal = BBYUtils.clone(this.pmReqItem.deal)
    }else {
      this.deal.messageId = this.pmReqItem.id
      this.deal.agree = BBYUtils.clone(this.pmReqItem.detail)
    }
    this.loadFileList()
    this.listLoading = false
  }

  getImgURL(file: FileInfo) {
    return BBYUtils.getTypeImg(file);
  }

  findGroup(id: string) {
    return this.groupList.find(item => item.id == id)
  }

  error(msg: string) {
    this.dialogService.error({nzTitle: '错误', nzContent: msg})
  }


  dealRun(data:PmMessageDeal){
    BBYUtils.handelResult(
      this.pmService.deal(data),
      res => {
        this.message.success('已完成！')
        if (this.pmReqItem) {
          this.pmReqItem.deal = data
        }
        this.submitLoading=false
        this.close()
      },
      res => {
        this.message.error(res.msg)
        this.submitLoading=false
      }
    )
  }
  rejectClick(){
    if (!this.pmReqItem) return;
    this.submitLoading=true
    let data:PmMessageDeal={
      messageId:this.pmReqItem.id,
      type:'reject'
    }
    this.dealRun(data);
  }
  applyClick() {
    if (this.deal.type != 'custom') {
      this.dealRun(this.deal)
      return;
    }
    if (!this.deal.customGroup||this.deal.customGroup.length==0) {
      this.error('未选择用户组！')
      return;
    }
    let groups:UserGroup[]=[]
    let userId = this.pmReqItem?.from || '';
    let flag = false;
    for (let id of this.deal.customGroup) {
      let group = this.findGroup(id);
      if (!group) continue;
      let content = group.content as any;
      if (content[userId]) {
        flag = true;
      }
      groups.push(group);
    }
    if (flag) {
      this.dealRun(this.deal)
    } else {
      this.addUserToSelGroup(groups)
    }
  }

  close() {
    console.log(this.dialogObj)
    this.dialogObj?.dialog?.destroy()
  }

  private addUserToSelGroup(groups:UserGroup[]) {
    this.dialogService.create({
      nzTitle:'添加用户到分组',
      nzContent:AddUserToGroup,
      nzComponentParams:{
        groups,
        userId:this.pmReqItem?.from||''
      },
      nzOnOk:(page)=>new Promise(resolve => {
        console.log(page)
        resolve()
      })
    })
  }

  private loadFileList() {
    if (!this.pmReqItem?.fileIds||this.pmReqItem?.fileIds.length==0){
      this.error('请求文件为空！')
      return;
    }

    BBYUtils.handelResult(
      this.diskService.fileInfos(this.pmReqItem?.fileIds),
      res => {
        console.log(res)
        this.fileList=res.data
      },
      res => {
        this.error('加载文件列表失败！')
      }
    )
  }
}
